<?php

class Categories extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{categories}}';
	}

	public function rules()
	{
		return array(
			array('category_name, category_root_id', 'required'),
			array('category_root_id', 'numerical', 'integerOnly'=>true),
			array('category_name', 'length', 'max'=>128),
			array('id, category_name, category_root_id', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_name' => 'Имя категории',
			'category_root_id' => 'Category Root',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('category_name',$this->category_name,true);
		$criteria->compare('category_root_id',$this->category_root_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function addImage($image)
    {
        $imgToCat=new ImageCategory;

        $imgToCat->attributes=array(
            'file_id'=>$image->id,
            'cat_id'=>$this->id,
        );

        return $imgToCat->save();
    }

    public function addFolder($dir)
    {
        $result=false;
        foreach($dir->files as $img){
            if($this->addImage($img)){
                $result=true;
            }
        }
        return $result;
    }

    public function getBreadcrumbs()
    {
        if($this->id==1){
            return 'Categories';
        }
        $brad=CHtml::link(CHtml::encode('Categories'), array('index', 'id'=>1));

        foreach($this->parents as $cat){
            if($cat){
                $brad.=DIRECTORY_SEPARATOR.CHtml::link(CHtml::encode($cat->name), array('index', 'id'=>$cat->id));
            }
        }
        return $brad.=DIRECTORY_SEPARATOR.$this->name;
    }

    public function getName()
    {
        return $this->category_name;
    }

    public function getRootId()
    {
        return $this->category_root_id;
    }

    public function getRoot()
    {
        return $this->findByPk($this->rootId);
    }

    public function getChildren()
    {
        return $this->findAllByAttributes(array('category_root_id'=>$this->id));
    }

    public function getUrl()
    {
        return Yii::app()->createUrl('categories/index', array(
            'id'=>$this->id,
        ));
    }

    public function getImages()
    {
        $id=$this->id;
        $files=new Files;
        $imageToCategory=new ImageCategory;
        $images=array();
        $imageIds=$imageToCategory->findAllByAttributes(array('cat_id'=>$id));

        foreach($imageIds as $img){
            if(is_file($files->findByPk($img->fileId)->location)){
                $images[]=$files->findByPk($img->fileId);
            }else{
                $img->delete();
            }
        }

        return $images;
    }

    protected function getParents()
    {
        $parents=array();
        $parentsLine=$this->category_parents;
        foreach(explode('/', $parentsLine) as $name){
            if($name){
                $parentsLine=str_replace(DIRECTORY_SEPARATOR.$name, '', $parentsLine);
                $parentsLine=str_replace($name, '', $parentsLine);
                $parentsLine=$parentsLine ? $parentsLine : 0;
                $parents[]=$this->findByAttributes(array(
                    'category_name'=>$name,
                    'category_parents'=>$parentsLine,
                ));
            }
        }

        return $parents;
    }
}